package com.ktjy.ktmall.controller;

import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.core.page.TableDataInfo;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.ktmall.domain.WxUser;
import com.ktjy.ktmall.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 微信小程序端的会员Controller
 *
 * @author ktjy
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/ktmall/user")
public class WxUserController extends BaseController {
    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询微信小程序端的会员列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxUser wxUser) {
        startPage();
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        return getDataTable(list);
    }


    /**
     * 导出微信小程序端的会员列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:export')")
    @Log(title = "微信小程序端的会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUser wxUser) {
        List<WxUser> list = wxUserService.list();
        ExcelUtil<WxUser> util = new ExcelUtil<WxUser>(WxUser.class);
        util.exportExcel(response, list, "微信小程序端的会员数据");
    }

    /**
     * 获取微信小程序端的会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxUserService.getById(id));
    }

    /**
     * 新增微信小程序端的会员
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:add')")
    @Log(title = "微信小程序端的会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUser wxUser) {
        return toAjax(wxUserService.save(wxUser));
    }

    /**
     * 修改微信小程序端的会员
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:edit')")
    @Log(title = "微信小程序端的会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUser wxUser) {
        return toAjax(wxUserService.updateById(wxUser));
    }

    /**
     * 删除微信小程序端的会员
     */
    @PreAuthorize("@ss.hasPermi('ktmall:user:remove')")
    @Log(title = "微信小程序端的会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids) {
        return toAjax(wxUserService.removeByIds(ids));
    }
}